1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003-2005 The GemRB Project
3 *
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.
8 
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.
13 
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.
17 *
18 *
19 */
20 
21 #include "ResourceManager.h"
22 
23 #include "Interface.h"
24 #include "PluginMgr.h"
25 #include "Resource.h"
26 #include "ResourceDesc.h"
27 #include "ResourceSource.h"
28 #include "System/StringBuffer.h"
29 
30 namespace GemRB {
31 
ResourceManager()32 ResourceManager::ResourceManager()
33 {
34 }
35 
36 
~ResourceManager()37 ResourceManager::~ResourceManager()
38 {
39 }
40 
AddSource(const char * path,const char * description,PluginID type,int flags)41 bool ResourceManager::AddSource(const char *path, const char *description, PluginID type, int flags)
42 {
43 	PluginHolder<ResourceSource> source(type);
44 	if (!source->Open(path, description)) {
45 		Log(WARNING, "ResourceManager", "Invalid path given: %s (%s)", path, description);
46 		return false;
47 	}
48 
49 	if (flags & RM_REPLACE_SAME_SOURCE) {
50 		for (size_t i = 0; i < searchPath.size(); i++) {
51 			if (!stricmp(description, searchPath[i]->GetDescription())) {
52 				searchPath[i] = source;
53 				break;
54 			}
55 		}
56 	} else {
57 		searchPath.push_back(source);
58 	}
59 	return true;
60 }
61 
PrintPossibleFiles(StringBuffer & buffer,const char * ResRef,const TypeID * type)62 static void PrintPossibleFiles(StringBuffer& buffer, const char* ResRef, const TypeID *type)
63 {
64 	const std::vector<ResourceDesc>& types = PluginMgr::Get()->GetResourceDesc(type);
65 	for (size_t j = 0; j < types.size(); j++) {
66 		buffer.appendFormatted("%s.%s ", ResRef, types[j].GetExt());
67 	}
68 }
69 
Exists(const char * ResRef,SClass_ID type,bool silent) const70 bool ResourceManager::Exists(const char *ResRef, SClass_ID type, bool silent) const
71 {
72 	if (!ResRef || ResRef[0] == '\0')
73 		return false;
74 	// TODO: check various caches
75 	for (size_t i = 0; i < searchPath.size(); i++) {
76 		if (searchPath[i]->HasResource( ResRef, type )) {
77 			return true;
78 		}
79 	}
80 	if (!silent) {
81 		Log(WARNING, "ResourceManager", "'%s.%s' not found...",
82 			ResRef, core->TypeExt(type));
83 	}
84 	return false;
85 }
86 
Exists(const char * ResRef,const TypeID * type,bool silent) const87 bool ResourceManager::Exists(const char *ResRef, const TypeID *type, bool silent) const
88 {
89 	if (ResRef[0] == '\0')
90 		return false;
91 	// TODO: check various caches
92 	const std::vector<ResourceDesc> &types = PluginMgr::Get()->GetResourceDesc(type);
93 	for (size_t j = 0; j < types.size(); j++) {
94 		for (size_t i = 0; i < searchPath.size(); i++) {
95 			if (searchPath[i]->HasResource(ResRef, types[j])) {
96 				return true;
97 			}
98 		}
99 	}
100 	if (!silent) {
101 		StringBuffer buffer;
102 		buffer.appendFormatted("Couldn't find '%s'... ", ResRef);
103 		buffer.append("Tried ");
104 		PrintPossibleFiles(buffer, ResRef,type);
105 		Log(WARNING, "ResourceManager", buffer);
106 	}
107 	return false;
108 }
109 
GetResource(const char * ResRef,SClass_ID type,bool silent) const110 DataStream* ResourceManager::GetResource(const char* ResRef, SClass_ID type, bool silent) const
111 {
112 	if (!ResRef || ResRef[0] == '\0')
113 		return NULL;
114 	for (size_t i = 0; i < searchPath.size(); i++) {
115 		DataStream *ds = searchPath[i]->GetResource(ResRef, type);
116 		if (ds) {
117 			if (!silent) {
118 				Log(MESSAGE, "ResourceManager", "Found '%s.%s' in '%s'.",
119 					ResRef, core->TypeExt(type), searchPath[i]->GetDescription());
120 			}
121 			return ds;
122 		}
123 	}
124 	if (!silent) {
125 		Log(ERROR, "ResourceManager", "Couldn't find '%s.%s'.",
126 			ResRef, core->TypeExt(type));
127 	}
128 	return NULL;
129 }
130 
GetResource(const char * ResRef,const TypeID * type,bool silent,bool useCorrupt) const131 Resource* ResourceManager::GetResource(const char* ResRef, const TypeID *type, bool silent, bool useCorrupt) const
132 {
133 	if (!ResRef || ResRef[0] == '\0')
134 		return NULL;
135 	if (!silent) {
136 		Log(MESSAGE, "ResourceManager", "Searching for '%s'...", ResRef);
137 	}
138 	const std::vector<ResourceDesc> &types = PluginMgr::Get()->GetResourceDesc(type);
139 	for (size_t j = 0; j < types.size(); j++) {
140 		for (size_t i = 0; i < searchPath.size(); i++) {
141 			DataStream *str = searchPath[i]->GetResource(ResRef, types[j]);
142 			if (!str && useCorrupt && core->UseCorruptedHack) {
143 				// don't look at other paths if requested
144 				core->UseCorruptedHack = false;
145 				return NULL;
146 			}
147 			core->UseCorruptedHack = false;
148 			if (str) {
149 				Resource *res = types[j].Create(str);
150 				if (res) {
151 					if (!silent) {
152 						Log(MESSAGE, "ResourceManager", "Found '%s.%s' in '%s'.",
153 							ResRef, types[j].GetExt(), searchPath[i]->GetDescription());
154 					}
155 					return res;
156 				}
157 			}
158 		}
159 	}
160 	if (!silent) {
161 		StringBuffer buffer;
162 		buffer.appendFormatted("Couldn't find '%s'... ", ResRef);
163 		buffer.append("Tried ");
164 		PrintPossibleFiles(buffer, ResRef,type);
165 		Log(WARNING, "ResourceManager", buffer);
166 	}
167 	return NULL;
168 }
169 
170 }
171