1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef PRINCE_RESOURCE_H
24 #define PRINCE_RESOURCE_H
25 
26 #include "common/stream.h"
27 #include "common/memstream.h"
28 #include "common/archive.h"
29 #include "common/debug-channels.h"
30 #include "common/ptr.h"
31 
32 #include "prince/decompress.h"
33 
34 namespace Prince {
35 
36 namespace Resource {
37 
38 Common::SeekableReadStream *getDecompressedStream(Common::SeekableReadStream *stream);
39 
40 template <typename T>
loadFromStream(T & resource,Common::SeekableReadStream & stream)41 bool loadFromStream(T &resource, Common::SeekableReadStream &stream) {
42 	return resource.loadStream(stream);
43 }
44 
45 template<typename T>
loadResource(T * resource,const char * resourceName,bool required)46 bool loadResource(T *resource, const char *resourceName, bool required) {
47 	Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
48 	if (!stream_) {
49 		if (required)
50 			error("Can't load %s", resourceName);
51 		return false;
52 	}
53 
54 	Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
55 
56 	return loadFromStream(*resource, *stream);
57 }
58 
59 template <typename T>
60 bool loadResource(Common::Array<T> &array, Common::SeekableReadStream &stream, bool required = true) {
61 	T t;
62 	while (t.loadFromStream(stream))
63 		array.push_back(t);
64 
65 	return true;
66 }
67 
68 
69 template <typename T>
70 bool loadResource(Common::Array<T> &array, const char *resourceName, bool required = true) {
71 	Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
72 	if (!stream_) {
73 		if (required)
74 			error("Can't load %s", resourceName);
75 		return false;
76 	}
77 
78 	Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
79 
80 	return loadResource(array, *stream, required);
81 }
82 
83 template <typename T>
84 bool loadResource(Common::Array<T *> &array, const char *resourceName, bool required = true) {
85 
86 	Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
87 	if (!stream_) {
88 		if (required)
89 			error("Can't load %s", resourceName);
90 		return false;
91 	}
92 
93 	Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
94 
95 	// FIXME: This is stupid. Maybe loadFromStream should be helper method that returns initialized object
96 	while (true) {
97 		T* t = new T();
98 		if (!t->loadFromStream(*stream)) {
99 			delete t;
100 			break;
101 		}
102 		array.push_back(t);
103 	}
104 	return true;
105 }
106 
107 }
108 
109 } // End of namespace Prince
110 
111 #endif
112