scummvm/engines/prince/resource.h

112 lines
3.1 KiB
C
Raw Normal View History

2013-12-05 00:02:31 +00:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2016-09-03 10:46:38 +00:00
*
2013-12-05 00:02:31 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2016-09-03 10:46:38 +00:00
*
2013-12-05 00:02:31 +00:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef PRINCE_RESOURCE_H
#define PRINCE_RESOURCE_H
#include "common/stream.h"
#include "common/memstream.h"
2013-12-05 00:02:31 +00:00
#include "common/archive.h"
#include "common/debug-channels.h"
2013-12-13 07:05:37 +00:00
#include "common/ptr.h"
2013-12-05 00:02:31 +00:00
#include "prince/decompress.h"
2013-12-05 00:02:31 +00:00
namespace Prince {
namespace Resource {
Common::SeekableReadStream *getDecompressedStream(Common::SeekableReadStream *stream);
2018-05-04 09:50:51 +00:00
template <typename T>
bool loadFromStream(T &resource, Common::SeekableReadStream &stream) {
return resource.loadStream(stream);
}
2013-12-05 00:02:31 +00:00
2018-05-04 09:50:51 +00:00
template<typename T>
bool loadResource(T *resource, const char *resourceName, bool required) {
Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
if (!stream_) {
2018-05-04 09:50:51 +00:00
if (required)
error("Can't load %s", resourceName);
return false;
2014-10-28 14:21:53 +00:00
}
2013-12-05 00:02:31 +00:00
Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
2018-05-04 09:50:51 +00:00
return loadFromStream(*resource, *stream);
}
2013-12-13 07:05:37 +00:00
2018-05-04 09:50:51 +00:00
template <typename T>
bool loadResource(Common::Array<T> &array, Common::SeekableReadStream &stream, bool required = true) {
T t;
while (t.loadFromStream(stream))
array.push_back(t);
2013-12-05 00:02:31 +00:00
2018-05-04 09:50:51 +00:00
return true;
}
2013-12-05 00:02:31 +00:00
2018-05-04 09:50:51 +00:00
template <typename T>
bool loadResource(Common::Array<T> &array, const char *resourceName, bool required = true) {
Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
if (!stream_) {
2018-05-04 09:50:51 +00:00
if (required)
error("Can't load %s", resourceName);
return false;
2013-12-05 00:02:31 +00:00
}
Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
2018-05-04 09:50:51 +00:00
return loadResource(array, *stream, required);
}
2013-12-13 07:05:37 +00:00
2018-05-04 09:50:51 +00:00
template <typename T>
bool loadResource(Common::Array<T *> &array, const char *resourceName, bool required = true) {
2013-12-05 00:02:31 +00:00
Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
if (!stream_) {
2018-05-04 09:50:51 +00:00
if (required)
error("Can't load %s", resourceName);
return false;
}
Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
2018-05-04 09:50:51 +00:00
// FIXME: This is stupid. Maybe loadFromStream should be helper method that returns initialized object
while (true) {
T* t = new T();
if (!t->loadFromStream(*stream)) {
delete t;
break;
2013-12-05 00:02:31 +00:00
}
2018-05-04 09:50:51 +00:00
array.push_back(t);
2013-12-05 00:02:31 +00:00
}
2018-05-04 09:50:51 +00:00
return true;
}
2013-12-13 07:05:37 +00:00
2013-12-05 00:02:31 +00:00
}
2014-08-01 15:38:04 +00:00
} // End of namespace Prince
2013-12-05 00:02:31 +00:00
#endif