Move EventMgr to GUI.
[gemrb.git] / gemrb / core / ResourceManager.cpp
blobede2c7295f65f71236d0f386b0db1cda967fb921
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"
23 #include "Interface.h"
24 #include "PluginMgr.h"
25 #include "Resource.h"
26 #include "ResourceDesc.h"
27 #include "ResourceSource.h"
29 ResourceManager::ResourceManager()
34 ResourceManager::~ResourceManager()
38 bool ResourceManager::AddSource(const char *path, const char *description, PluginID type)
40 PluginHolder<ResourceSource> source(type);
41 if (!source->Open(path, description)) {
42 return false;
44 searchPath.push_back(source);
45 return true;
48 static void PrintPossibleFiles(const char* ResRef, const TypeID *type)
50 const std::vector<ResourceDesc>& types = PluginMgr::Get()->GetResourceDesc(type);
51 for (size_t j = 0; j < types.size(); j++) {
52 printf("%s%s ", ResRef, types[j].GetExt());
56 bool ResourceManager::Exists(const char *ResRef, SClass_ID type, bool silent) const
58 if (ResRef[0] == '\0')
59 return false;
60 // TODO: check various caches
61 for (size_t i = 0; i < searchPath.size(); i++) {
62 if (searchPath[i]->HasResource( ResRef, type )) {
63 return true;
66 if (!silent) {
67 printMessage( "ResourceManager", "Searching for ", WHITE );
68 printf( "%s%s...", ResRef, core->TypeExt( type ) );
69 printStatus( "NOT FOUND", YELLOW );
71 return false;
74 bool ResourceManager::Exists(const char *ResRef, const TypeID *type, bool silent) const
76 if (ResRef[0] == '\0')
77 return false;
78 // TODO: check various caches
79 const std::vector<ResourceDesc> &types = PluginMgr::Get()->GetResourceDesc(type);
80 for (size_t j = 0; j < types.size(); j++) {
81 for (size_t i = 0; i < searchPath.size(); i++) {
82 if (searchPath[i]->HasResource(ResRef, types[j])) {
83 return true;
87 if (!silent) {
88 printMessage( "ResourceManager", "Searching for ", WHITE );
89 printf( "%s... ", ResRef );
90 printf("Tried ");
91 PrintPossibleFiles(ResRef,type);
92 printStatus( "NOT FOUND", YELLOW );
94 return false;
97 DataStream* ResourceManager::GetResource(const char* ResRef, SClass_ID type, bool silent) const
99 if (ResRef[0] == '\0')
100 return NULL;
101 if (!silent) {
102 printMessage( "ResourceManager", "Searching for ", WHITE );
103 printf( "%s%s...", ResRef, core->TypeExt( type ) );
105 for (size_t i = 0; i < searchPath.size(); i++) {
106 DataStream *ds = searchPath[i]->GetResource(ResRef, type);
107 if (ds) {
108 if (!silent) {
109 printStatus( searchPath[i]->GetDescription(), GREEN );
111 return ds;
114 if (!silent) {
115 printStatus( "ERROR", LIGHT_RED );
117 return NULL;
120 Resource* ResourceManager::GetResource(const char* ResRef, const TypeID *type, bool silent) const
122 if (ResRef[0] == '\0')
123 return NULL;
124 if (!silent) {
125 printMessage( "ResourceManager", "Searching for ", WHITE );
126 printf( "%s... ", ResRef );
128 const std::vector<ResourceDesc> &types = PluginMgr::Get()->GetResourceDesc(type);
129 for (size_t j = 0; j < types.size(); j++) {
130 for (size_t i = 0; i < searchPath.size(); i++) {
131 DataStream *str = searchPath[i]->GetResource(ResRef, types[j]);
132 if (str) {
133 Resource *res = types[j].Create(str);
134 if (res) {
135 if (!silent) {
136 printf( "%s%s...", ResRef, types[j].GetExt() );
137 printStatus( searchPath[i]->GetDescription(), GREEN );
139 return res;
144 if (!silent) {
145 printf("Tried ");
146 PrintPossibleFiles(ResRef,type);
147 printStatus( "ERROR", LIGHT_RED );
149 return NULL;