Merge branch 'sound'
[gemrb.git] / gemrb / core / ResourceManager.cpp
blobc4e19c17fed3b94a9e0fc912541db4d1c5d0855d
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003-2005 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "ResourceManager.h"
22 #include "Resource.h"
23 #include "ResourceDesc.h"
24 #include "ResourceSource.h"
25 #include "Interface.h"
26 #include "PluginMgr.h"
28 ResourceManager::ResourceManager()
33 ResourceManager::~ResourceManager()
35 std::vector<ResourceSource*>::iterator i;
36 for (i = searchPath.begin(); i != searchPath.end(); ++i) {
37 if (*i)
38 (*i)->release();
42 bool ResourceManager::AddSource(char *path, const char *description, PluginID type)
44 ResourceSource *source = ( ResourceSource * ) core->GetInterface( type );
45 if (!source->Open(path, description)) {
46 source->release();
47 return false;
49 searchPath.push_back(source);
50 return true;
53 static void PrintPossibleFiles(const char* ResRef, const TypeID *type)
55 const std::vector<ResourceDesc>& types = PluginMgr::Get()->GetResourceDesc(type);
56 for (size_t j = 0; j < types.size(); j++) {
57 printf("%s%s ", ResRef, types[j].GetExt());
61 bool ResourceManager::Exists(const char *ResRef, SClass_ID type, bool silent)
63 if (ResRef[0] == '\0')
64 return false;
65 // TODO: check various caches
66 for (size_t i = 0; i < searchPath.size(); i++) {
67 if (searchPath[i]->HasResource( ResRef, type )) {
68 return true;
71 if (!silent) {
72 printMessage( "ResourceManager", "Searching for ", WHITE );
73 printf( "%s%s...", ResRef, core->TypeExt( type ) );
74 printStatus( "NOT FOUND", YELLOW );
76 return false;
79 bool ResourceManager::Exists(const char *ResRef, const TypeID *type, bool silent)
81 if (ResRef[0] == '\0')
82 return false;
83 // TODO: check various caches
84 const std::vector<ResourceDesc> &types = PluginMgr::Get()->GetResourceDesc(type);
85 for (size_t j = 0; j < types.size(); j++) {
86 for (size_t i = 0; i < searchPath.size(); i++) {
87 if (searchPath[i]->HasResource(ResRef, types[j])) {
88 return true;
92 if (!silent) {
93 printMessage( "ResourceManager", "Searching for ", WHITE );
94 printf( "%s... ", ResRef );
95 printf("Tried ");
96 PrintPossibleFiles(ResRef,type);
97 printStatus( "NOT FOUND", YELLOW );
99 return false;
102 DataStream* ResourceManager::GetResource(const char* ResRef, SClass_ID type, bool silent) const
104 if (ResRef[0] == '\0')
105 return false;
106 if (!silent) {
107 printMessage( "ResourceManager", "Searching for ", WHITE );
108 printf( "%s%s...", ResRef, core->TypeExt( type ) );
110 for (size_t i = 0; i < searchPath.size(); i++) {
111 DataStream *ds = searchPath[i]->GetResource(ResRef, type);
112 if (ds) {
113 if (!silent) {
114 printStatus( searchPath[i]->GetDescription(), GREEN );
116 return ds;
119 if (!silent) {
120 printStatus( "ERROR", LIGHT_RED );
122 return NULL;
125 Resource* ResourceManager::GetResource(const char* ResRef, const TypeID *type, bool silent) const
127 if (ResRef[0] == '\0')
128 return false;
129 if (!silent) {
130 printMessage( "ResourceManager", "Searching for ", WHITE );
131 printf( "%.8s... ", ResRef );
133 const std::vector<ResourceDesc> &types = PluginMgr::Get()->GetResourceDesc(type);
134 for (size_t j = 0; j < types.size(); j++) {
135 for (size_t i = 0; i < searchPath.size(); i++) {
136 DataStream *str = searchPath[i]->GetResource(ResRef, types[j]);
137 if (str) {
138 Resource *res = types[j].Create(str);
139 if (res) {
140 if (!silent) {
141 printf( "%s%s...", ResRef, types[j].GetExt() );
142 printStatus( searchPath[i]->GetDescription(), GREEN );
144 return res;
149 if (!silent) {
150 printf("Tried ");
151 PrintPossibleFiles(ResRef,type);
152 printStatus( "ERROR", LIGHT_RED );
154 return NULL;