ScummVM API documentation
object.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef STARK_RESOURCES_RESOURCE_H
23 #define STARK_RESOURCES_RESOURCE_H
24 
25 #include "common/array.h"
26 #include "common/str.h"
27 
28 namespace Stark {
29 
30 namespace Formats {
31 class XRCReadStream;
32 }
33 class ResourceSerializer;
34 
35 namespace Resources {
36 
37 class Type {
38 public:
39  enum ResourceType {
40  kInvalid = 0,
41  kRoot = 1,
42  kLevel = 2,
43  kLocation = 3,
44  kLayer = 4,
45  kCamera = 5,
46  kFloor = 6,
47  kFloorFace = 7,
48  kItem = 8,
49  kScript = 9,
50  kAnimHierarchy = 10,
51  kAnim = 11,
52  kDirection = 12,
53  kImage = 13,
54  kAnimScript = 14,
55  kAnimScriptItem = 15,
56  kSoundItem = 16,
57  kPath = 17,
58  kFloorField = 18,
59  kBookmark = 19,
60  kKnowledgeSet = 20,
61  kKnowledge = 21,
62  kCommand = 22,
63  kPATTable = 23,
64  kContainer = 26,
65  kDialog = 27,
66  kSpeech = 29,
67  kLight = 30,
68  kCursor = 31, // Not sure about this one
69  kBonesMesh = 32,
70  kScroll = 33,
71  kFMV = 34,
72  kLipSync = 35,
73  kAnimSoundTrigger = 36,
74  kString = 37,
75  kTextureSet = 38
76  };
77 
78  Type();
79  Type(ResourceType type);
80 
81  ResourceType get() const;
82  const char *getName() const;
83 
84  bool operator==(const Type &other) const {
85  return other._type == _type;
86  }
87 
88  bool operator!=(const Type &other) const {
89  return other._type != _type;
90  }
91 
92  bool operator==(const Type::ResourceType other) const {
93  return other == _type;
94  }
95 
96  bool operator!=(const Type::ResourceType other) const {
97  return other != _type;
98  }
99 
100 private:
101  ResourceType _type;
102 };
103 
143 class Object {
144 public:
145  virtual ~Object();
146 
148  Type getType() const { return _type; }
149 
151  byte getSubType() const { return _subType; }
152 
154  uint16 getIndex() const { return _index; }
155 
157  Common::String getIndexAsString() const { return Common::String::format("%02x", _index); }
158 
160  Common::String getName() const { return _name; }
161 
165  virtual void readData(Formats::XRCReadStream *stream);
166 
170  virtual void saveLoad(ResourceSerializer *serializer);
171 
177  virtual void saveLoadCurrent(ResourceSerializer *serializer);
178 
184  virtual void onPostRead();
185 
191  virtual void onAllLoaded();
192 
196  virtual void onEnterLocation();
197 
201  virtual void onGameLoop();
202 
206  virtual void onEnginePause(bool pause);
207 
211  virtual void onExitLocation();
212 
216  virtual void onPreDestroy();
217 
221  template<class T>
222  static T *cast(Object *resource);
223 
225  template<class T>
226  T *findParent();
227 
229  Object *findChildWithIndex(Type type, uint16 index, int subType = -1) const;
230 
232  Object *findChildWithOrder(Type type, uint16 order, int subType = -1) const;
233 
235  Object *findChildWithName(Type type, const Common::String &name, int subType = -1) const;
236 
238  template<class T>
239  T *findChild(bool mustBeUnique = true) const;
240 
242  template<class T>
243  T *findChildWithSubtype(int subType, bool mustBeUnique = true) const;
244 
246  template<class T>
247  T *findChildWithIndex(uint16 index, int subType = -1) const;
248 
250  template<class T>
251  T *findChildWithOrder(uint16 order, int subType = -1) const;
252 
254  template<class T>
255  T *findChildWithName(const Common::String &name, int subType = -1) const;
256 
258  template<class T>
259  Common::Array<T *> listChildren(int subType = -1) const;
260 
262  template<class T>
263  Common::Array<T *> listChildrenRecursive(int subType = -1);
264 
266  void addChild(Object *child);
267 
269  virtual void print(uint depth = 0);
270 
271 protected:
272  Object(Object *parent, byte subType, uint16 index, const Common::String &name);
273 
274  void printWithDepth(uint depth, const Common::String &string) const;
275  void printDescription(uint depth) const;
276  virtual void printData();
277 
278  Type _type;
279  byte _subType;
280  uint16 _index;
281  Common::String _name;
282 
283  Object *_parent;
284  Common::Array<Object *> _children;
285 };
286 
294 public:
295  UnimplementedResource(Object *parent, Type type, byte subType, uint16 index, const Common::String &name);
296  virtual ~UnimplementedResource();
297 
298 protected:
299  void readData(Formats::XRCReadStream *stream) override;
300  void printData() override;
301 
302  uint32 _dataLength;
303  byte *_data;
304 };
305 
306 template <class T>
307 T* Object::cast(Object *resource) {
308  if (resource && resource->_type != T::TYPE) {
309  error("Unexpected resource type when casting resource %s instead of %s",
310  resource->_type.getName(), Type(T::TYPE).getName());
311  }
312 
313  return (T *) resource;
314 }
315 
316 template<>
317 Object *Object::cast<Object>(Object *resource);
318 
319 template<class T>
320 T *Object::findParent() {
321  if (getType() == T::TYPE) {
322  return cast<T>(this);
323  } else if (!_parent) {
324  return nullptr;
325  } else {
326  return _parent->findParent<T>();
327  }
328 }
329 
330 template<>
331 Object *Object::findParent();
332 
333 template <class T>
334 Common::Array<T *> Object::listChildren(int subType) const {
335  Common::Array<T *> list;
336 
337  for (uint i = 0; i < _children.size(); i++) {
338  if (_children[i]->getType() == T::TYPE
339  && (_children[i]->getSubType() == subType || subType == -1)) {
340  // Found a matching child
341  list.push_back(Object::cast<T>(_children[i]));
342  }
343  }
344 
345  return list;
346 }
347 
348 template<class T>
349 Common::Array<T *> Object::listChildrenRecursive(int subType) {
350  Common::Array<T *> list;
351 
352  for (uint i = 0; i < _children.size(); i++) {
353  if (_children[i]->getType() == T::TYPE
354  && (_children[i]->getSubType() == subType || subType == -1)) {
355  // Found a matching child
356  list.push_back(Object::cast<T>(_children[i]));
357  }
358 
359  // Look for matching resources in the child's children
360  list.push_back(_children[i]->listChildrenRecursive<T>(subType));
361  }
362 
363  return list;
364 }
365 
366 template<>
367 Common::Array<Object *> Object::listChildren<Object>(int subType) const;
368 
369 template<class T>
370 T *Object::findChild(bool mustBeUnique) const {
371  return findChildWithSubtype<T>(-1, mustBeUnique);
372 }
373 
374 template <class T>
375 T *Object::findChildWithSubtype(int subType, bool mustBeUnique) const {
376  Common::Array<T *> list = listChildren<T>(subType);
377 
378  if (list.empty()) {
379  return nullptr;
380  }
381 
382  if (list.size() > 1 && mustBeUnique) {
383  error("Several children resources matching criteria type = %s, subtype = %d", Type(T::TYPE).getName(), subType);
384  }
385 
386  return list.front();
387 }
388 
389 template <class T>
390 T *Object::findChildWithIndex(uint16 index, int subType) const {
391  return Object::cast<T>(findChildWithIndex(T::TYPE, index, subType));
392 }
393 
394 template <class T>
395 T *Object::findChildWithOrder(uint16 order, int subType) const {
396  return Object::cast<T>(findChildWithOrder(T::TYPE, order, subType));
397 }
398 
399 template<class T>
400 T *Object::findChildWithName(const Common::String &name, int subType) const {
401  return Object::cast<T>(findChildWithName(T::TYPE, name, subType));
402 }
403 
404 } // End of namespace Resources
405 } // End of namespace Stark
406 
407 #endif // STARK_RESOURCES_RESOURCE_H
Definition: object.h:37
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Common::String getIndexAsString() const
Definition: object.h:157
Common::String getName() const
Definition: object.h:160
Definition: array.h:52
T & front()
Definition: array.h:217
Type
Definition: log.h:33
bool empty() const
Definition: array.h:351
void push_back(const T &element)
Definition: array.h:180
T * findParent()
Definition: object.h:320
Definition: console.h:27
Definition: object.h:143
size_type size() const
Definition: array.h:315
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
byte getSubType() const
Definition: object.h:151
Type getType() const
Definition: object.h:148
Definition: xrc.h:45
Definition: stateprovider.h:51
uint16 getIndex() const
Definition: object.h:154