ScummVM API documentation
utils.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 NGI_UTILS_H
23 #define NGI_UTILS_H
24 
25 #include "common/hash-ptr.h"
26 #include "common/hash-str.h"
27 #include "common/array.h"
28 #include "common/file.h"
29 
30 namespace NGI {
31 
32 class CObject;
33 class NGIArchive;
34 
35 typedef Common::HashMap<void *, int> ObjHash;
36 
38 
40  ClassMap _classMap;
41  Common::Array<CObject *> _objectMap;
42  Common::Array<int> _objectIdMap;
43  ObjHash _objectHash;
44 
45  int _lastIndex;
46  int _level;
47 
49  Common::WriteStream *_wstream;
50 
51 public:
54 
55  Common::String readPascalString(bool twoByte = false);
56  void writePascalString(const Common::String &str, bool twoByte = false);
57  int readCount();
58  CObject *parseClass(bool *isCopyReturned);
59 
61  template <typename T>
62  T *readClass() {
63  CObject *obj = readBaseClass();
64  if (!obj)
65  return nullptr;
66 
67  T *res = dynamic_cast<T *>(obj);
68  assert(res);
69  return res;
70  }
71 
72  void writeObject(CObject *obj);
73 
74  void incLevel() { _level++; }
75  void decLevel() { _level--; }
76  int getLevel() { return _level; }
77 
78  bool eos() const override { return _stream->eos(); }
79  uint32 read(void *dataPtr, uint32 dataSize) override { return _stream->read(dataPtr, dataSize); }
80  int64 pos() const override { return _stream ? _stream->pos() : _wstream->pos(); }
81  int64 size() const override { return _stream->size(); }
82  bool seek(int64 offset, int whence = SEEK_SET) override { return _stream->seek(offset, whence); }
83 
84  uint32 write(const void *dataPtr, uint32 dataSize) override { return _wstream->write(dataPtr, dataSize); }
85 
86 private:
87  void init();
88  CObject *readBaseClass();
89 };
90 
91 enum ObjType {
92  kObjTypeDefault,
93  kObjTypeExCommand,
94  kObjTypeExCommand2,
95  kObjTypeModalSaveGame,
96  kObjTypeMovGraph,
97  kObjTypeMovGraphLink,
98  kObjTypeMovGraphNode,
99  kObjTypeMctlCompound,
100  kObjTypeObjstateCommand,
101  kObjTypePictureObject,
102  kObjTypeStaticANIObject,
103  kObjTypeGameVar
104 };
105 
106 class CObject {
107 public:
108  ObjType _objtype;
109  uint _cnum;
110 
111  CObject() : _objtype(kObjTypeDefault), _cnum(0) {}
112  virtual bool load(MfcArchive &in) { return true; }
113  virtual void save(MfcArchive &out) { error("Not implemented for obj type: %d", _objtype); }
114  virtual ~CObject() {}
115 
116  bool loadFile(const Common::Path &fname);
117 };
118 
119 template <class T>
120 class ObList : public Common::List<T *>, public CObject {
121 public:
122  bool load(MfcArchive &file) override {
123  debugC(5, kDebugLoading, "ObList::load()");
124  int count = file.readCount();
125 
126  debugC(9, kDebugLoading, "ObList::count: %d:", count);
127 
128  for (int i = 0; i < count; i++) {
129  debugC(9, kDebugLoading, "ObList::[%d]", i);
130  T *t = file.readClass<T>();
131 
132  this->push_back(t);
133  }
134 
135  return true;
136  }
137 };
138 
140  friend class Picture;
141  friend class Scene;
142 
143  protected:
144  Common::Path _memfilename;
145  int _mfield_8;
146  int _mfield_C;
147  int _mfield_10;
148  char _mfield_14;
149  byte *_data;
150  int _dataSize;
151  int _mflags;
152  NGIArchive *_libHandle;
153 
154  public:
155  MemoryObject();
156  ~MemoryObject() override;
157 
158  bool load(MfcArchive &file) override;
159  void loadFile(const Common::Path &filename);
160  void load() { loadFile(_memfilename); }
161  byte *getData();
162  byte *loadData();
163  int getDataSize() const { return _dataSize; }
164 
165  bool testFlags();
166 
167  void freeData();
168 };
169 
170 class MemoryObject2 : public MemoryObject {
171  friend class Picture;
172 
173  protected:
174  byte **_rows;
175 
176  public:
177  MemoryObject2();
178  ~MemoryObject2() override;
179  bool load(MfcArchive &file) override;
180 
181  void copyData(byte *src, int dataSize);
182 };
183 
184 class ObArray : public Common::Array<CObject>, public CObject {
185  public:
186  bool load(MfcArchive &file) override;
187 };
188 
189 class DWordArray : public Common::Array<int32>, public CObject {
190  public:
191  bool load(MfcArchive &file) override;
192 };
193 
194 Common::Path genFileName(int superId, int sceneId, const char *ext);
195 byte *transCyrillic(const Common::String &str);
196 
197 } // End of namespace NGI
198 
199 #endif /* NGI_UTILS_H */
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: utils.h:79
virtual int64 size() const =0
Definition: str.h:59
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: utils.h:84
T * readClass()
Definition: utils.h:62
virtual int64 pos() const =0
Definition: utils.h:184
Definition: utils.h:39
Definition: stream.h:77
int64 pos() const override
Definition: utils.h:80
Definition: array.h:52
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
virtual bool eos() const =0
int64 size() const override
Definition: utils.h:81
Definition: list.h:44
Definition: path.h:52
Definition: stream.h:745
Definition: ngiarchive.h:44
virtual int64 pos() const =0
Definition: gfx.h:80
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: utils.h:82
bool eos() const override
Definition: utils.h:78
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: scene.h:32
Definition: anihandler.h:25
Definition: utils.h:120
void void void void void debugC(int level, uint32 debugChannels, MSVC_PRINTF const char *s,...) GCC_PRINTF(3
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
Definition: utils.h:189
Definition: utils.h:139
Definition: utils.h:106
Definition: utils.h:170
virtual uint32 read(void *dataPtr, uint32 dataSize)=0