ScummVM API documentation
xfile_loader.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 /*
23  * Partially based on XFile parser code from Wine sources.
24  * Copyright 2008 Christian Costa
25  */
26 
27 #ifndef WINTERMUTE_XFILE_LOADER_H
28 #define WINTERMUTE_XFILE_LOADER_H
29 
30 #include "common/str.h"
31 #include "common/stack.h"
32 
33 namespace Wintermute {
34 
35 enum XTokenType : uint16 {
36  XTOKEN_ERROR = 0xffff,
37  XTOKEN_NONE = 0,
38  XTOKEN_NAME = 1,
39  XTOKEN_STRING = 2,
40  XTOKEN_INTEGER = 3,
41  XTOKEN_GUID = 5,
42  XTOKEN_INTEGER_LIST = 6,
43  XTOKEN_FLOAT_LIST = 7,
44  XTOKEN_OBRACE = 10,
45  XTOKEN_CBRACE = 11,
46  XTOKEN_OPAREN = 12,
47  XTOKEN_CPAREN = 13,
48  XTOKEN_OBRACKET = 14,
49  XTOKEN_CBRACKET = 15,
50  XTOKEN_OANGLE = 16,
51  XTOKEN_CANGLE = 17,
52  XTOKEN_DOT = 18,
53  XTOKEN_COMMA = 19,
54  XTOKEN_SEMICOLON = 20,
55  XTOKEN_TEMPLATE = 31,
56  XTOKEN_WORD = 40,
57  XTOKEN_DWORD = 41,
58  XTOKEN_FLOAT = 42,
59  XTOKEN_DOUBLE = 43,
60  XTOKEN_CHAR = 44,
61  XTOKEN_UCHAR = 45,
62  XTOKEN_SWORD = 46,
63  XTOKEN_SDWORD = 47,
64  XTOKEN_VOID = 48,
65  XTOKEN_LPSTR = 49,
66  XTOKEN_UNICODE = 50,
67  XTOKEN_CSTRING = 51,
68  XTOKEN_ARRAY = 52
69 };
70 
71 #define XMAX_NAME_LEN 120
72 #define XMAX_STRING_LEN 500
73 
74 struct XToken {
75  XTokenType _type;
76  char _textVal[XMAX_STRING_LEN];
77  uint32 _integerVal;
78  float _floatVal;
79 };
80 
81 struct XVector {
82  float _x;
83  float _y;
84  float _z;
85 };
86 
87 struct XCoords2d {
88  float _u;
89  float _v;
90 };
91 
92 struct XMeshFace {
93  uint32 _numFaceVertexIndices;
94  uint32 _faceVertexIndices[4];
95 };
96 
98  float _time;
99  uint32 _numTfkeys;
100  float _tfkeys[16];
101 };
102 
104  uint32 _index;
105  float _indexColorR;
106  float _indexColorG;
107  float _indexColorB;
108  float _indexColorA;
109 };
110 
112  uint32 _type;
113  uint32 _method;
114  uint32 _usage;
115  uint32 _usageIndex;
116 };
117 
119  uint32 _nMaterials;
120  uint32 _numFaceIndexes;
121  uint32 *_faceIndexes{};
122 
124  delete[] _faceIndexes;
125  }
126 };
127 
129  uint32 _nOriginalVertices;
130  uint32 _numIndices;
131  uint32 *_indices{};
132 
134  delete[] _indices;
135  }
136 };
137 
139  uint32 _nMaxSkinWeightsPerVertex;
140  uint32 _nMaxSkinWeightsPerFace;
141  uint32 _nBones;
142 };
143 
145  char _transformNodeName[XMAX_NAME_LEN];
146  uint32 _numVertexIndices;
147  uint32 *_vertexIndices{};
148  uint32 _numWeights;
149  float *_weights{};
150  float _matrixOffset[16];
151 
152  ~XSkinWeightsObject() {
153  delete[] _vertexIndices;
154  delete[] _weights;
155  }
156 };
157 
158 struct XMeshObject {
159  uint32 _numVertices;
160  XVector *_vertices{};
161  uint32 _numFaces;
162  XMeshFace *_faces{};
163 
164  ~XMeshObject() {
165  delete[] _vertices;
166  delete[] _faces;
167  }
168 };
169 
171  uint32 _numNormals;
172  XVector *_normals{};
173  uint32 _numFaceNormals;
174  XMeshFace *_faceNormals{};
175 
176  ~XMeshNormalsObject() {
177  delete[] _normals;
178  delete[] _faceNormals;
179  }
180 };
181 
183  uint32 _numVertexColors;
184  XIndexedColor *_vertexColors{};
185 
187  delete[] _vertexColors;
188  }
189 };
190 
192  uint32 _numTextureCoords;
193  XCoords2d *_textureCoords{};
194 
196  delete[] _textureCoords;
197  }
198 };
199 
201  float _colorR;
202  float _colorG;
203  float _colorB;
204  float _colorA;
205  float _power;
206  float _specularR;
207  float _specularG;
208  float _specularB;
209  float _emissiveR;
210  float _emissiveG;
211  float _emissiveB;
212 };
213 
215  char _filename[XMAX_NAME_LEN];
216 };
217 
219  uint32 _animTicksPerSecond;
220 };
221 
223 };
224 
226 };
227 
229  uint32 _keyType;
230  uint32 _numKeys;
231  XTimedFloatKeys *_keys{};
232 
234  delete[] _keys;
235  }
236 };
237 
239  uint32 _openclosed;
240  uint32 _positionquality;
241 };
242 
243 struct XFrameObject {
244 };
245 
247  float _frameMatrix[16];
248 };
249 
251  uint32 _numElements;
252  XVertexElement *_elements{};
253  uint32 _numData;
254  uint32 *_data{};
255 
256  ~XDeclDataObject() {
257  delete[] _elements;
258  delete[] _data;
259  }
260 };
261 
263  uint32 _dwFVF;
264  uint32 _numData;
265  uint32 *_data{};
266 
267  ~XFVFDataObject() {
268  delete[] _data;
269  }
270 };
271 
272 enum XClassType {
273  kXClassUnknown = 0,
274  kXClassAnimTicksPerSecond,
275  kXClassFrameTransformMatrix,
276  kXClassFrame,
277  kXClassMesh,
278  kXClassMeshNormals,
279  kXClassMeshVertexColors,
280  kXClassMeshTextureCoords,
281  kXClassMeshMaterialList,
282  kXClassVertexDuplicationIndices,
283  kXClassMaterial,
284  kXClassTextureFilename,
285  kXClassSkinMeshHeader,
286  kXClassSkinWeights,
287  kXClassAnimationSet,
288  kXClassAnimation,
289  kXClassAnimationKey,
290  kXClassAnimationOptions,
291  kXClassDeclData,
292  kXClassFVFData,
293 };
294 
295 class XFileEnumObject;
296 
297 class XObject {
298  friend class XFileLoader;
299  friend class XFileData;
300  friend class XFileEnumObject;
301 
302 private:
303 
304  Common::String _name;
305  XClassType _classType{};
306  void *_object{};
307  XObject *_targetObject{};
308  Common::Stack<XObject *> _children;
309 
310 public:
311 
312  void deinit() {
313  switch (_classType) {
314  case kXClassAnimTicksPerSecond:
315  delete (XAnimTicksPerSecondObject *)_object;
316  break;
317  case kXClassAnimationKey:
318  delete (XAnimationKeyObject *)_object;
319  break;
320  case kXClassAnimation:
321  delete (XAnimationObject *)_object;
322  break;
323  case kXClassAnimationOptions:
324  delete (XAnimationOptionsObject *)_object;
325  break;
326  case kXClassAnimationSet:
327  delete (XAnimationSetObject *)_object;
328  break;
329  case kXClassDeclData:
330  delete (XDeclDataObject *)_object;
331  break;
332  case kXClassFrame:
333  delete (XFrameObject *)_object;
334  break;
335  case kXClassFrameTransformMatrix:
336  delete (XFrameTransformMatrixObject *)_object;
337  break;
338  case kXClassFVFData:
339  delete (XFVFDataObject *)_object;
340  break;
341  case kXClassMaterial:
342  delete (XMaterialObject *)_object;
343  break;
344  case kXClassMesh:
345  delete (XMeshObject *)_object;
346  break;
347  case kXClassMeshMaterialList:
348  delete (XMeshMaterialListObject *)_object;
349  break;
350  case kXClassMeshNormals:
351  delete (XMeshNormalsObject *)_object;
352  break;
353  case kXClassMeshVertexColors:
354  delete (XMeshVertexColorsObject *)_object;
355  break;
356  case kXClassMeshTextureCoords:
357  delete (XMeshTextureCoordsObject *)_object;
358  break;
359  case kXClassSkinMeshHeader:
360  delete (XSkinMeshHeaderObject *)_object;
361  break;
362  case kXClassSkinWeights:
363  delete (XSkinWeightsObject *)_object;
364  break;
365  case kXClassVertexDuplicationIndices:
366  delete (XVertexDuplicationIndicesObject *)_object;
367  break;
368  case kXClassTextureFilename:
369  delete (XTextureFilenameObject *)_object;
370  break;
371  case kXClassUnknown:
372  break;
373  }
374  }
375 };
376 
377 class XFileLoader {
378  friend class XFileEnumObject;
379 
380 private:
381 
382  const int kCabBlockSize = 0x8000;
383  const int kCabInputmax = kCabBlockSize + 12;
384 
385  bool _initialised{};
386  XToken _currentToken;
387  byte *_decompBuffer{};
388  byte *_buffer;
389  uint32 _bufferLeft;
390  bool _isText;
391  uint32 _listNbElements;
392  bool _listTypeFloat;
393  bool _listSeparator;
394  bool _tokenPresent;
395 
396  Common::Stack<XObject *> _xobjects;
397 
398 public:
399 
400  XFileLoader();
401  ~XFileLoader();
402  bool load(byte *buffer, uint32 bufferSize);
403  bool createEnumObject(XFileEnumObject &xobj);
404 
405 private:
406 
407  void init();
408  void deinit();
409 
410  FORCEINLINE bool readChar(char &c);
411  FORCEINLINE void rewindBytes(uint32 size);
412  bool readBytes(void *data, uint32 size);
413  bool readLE16(uint16 *data);
414  bool readLE32(uint32 *data);
415  bool readBE32(uint32 *data);
416 
417  FORCEINLINE bool getInteger(uint32 &value);
418  FORCEINLINE bool getFloat(float &value);
419  FORCEINLINE bool getString(char *str, uint maxLen);
420  FORCEINLINE bool skipSemicolonComma();
421 
422  FORCEINLINE bool isSpace(char c);
423  FORCEINLINE bool isOperator(char c);
424  FORCEINLINE bool isSeparator(char c);
425  FORCEINLINE bool isPrimitiveType(XTokenType token);
426  FORCEINLINE bool isGuid();
427  FORCEINLINE bool isName();
428  FORCEINLINE bool isFloat();
429  FORCEINLINE bool isInteger();
430  FORCEINLINE bool isString();
431  FORCEINLINE bool isKeyword(const char *keyword, uint len);
432  FORCEINLINE XTokenType getKeywordToken();
433  FORCEINLINE XTokenType checkToken();
434  XTokenType getToken();
435  void parseToken();
436 
437  bool decompressMsZipData();
438 
439  bool parseHeader();
440 
441  bool parseTemplate();
442  bool parseTemplateParts();
443  bool parseTemplateOptionInfo();
444  bool parseTemplateMembersList();
445  XObject *resolveChildObject(XObject *object, const Common::String &referenceName);
446  bool resolveObject(XObject *referenceObject, const Common::String &referenceName);
447  bool parseObject(XObject *object);
448  bool parseChildObjects(XObject *object);
449  bool parseObjectParts(XObject *object);
450 };
451 
452 class XFileData {
453  friend class XFileEnumObject;
454 
455 private:
456 
457  XObject *_xobject{};
458  bool _reference{};
459 
460 public:
461 
462  bool getChild(uint id, XFileData &child) {
463  if (_xobject) {
464  if (id < _xobject->_children.size()) {
465  child._xobject = _xobject->_children[id];
466  if (child._xobject->_targetObject) {
467  child._xobject = child._xobject->_targetObject;
468  child._reference = true;
469  }
470  return true;
471  }
472  }
473  return false;
474  }
475 
476  bool getChildren(uint &num) {
477  if (_xobject) {
478  num = _xobject->_children.size();
479  return true;
480  }
481  return false;
482  }
483 
484  bool getName(Common::String &name) {
485  if (_xobject) {
486  name = _xobject->_name;
487  return true;
488  }
489  return false;
490  }
491 
492  bool getType(XClassType &classType) {
493  if (_xobject) {
494  classType = _xobject->_classType;
495  return true;
496  }
497  return false;
498  }
499 
500  bool isReference() {
501  if (_xobject) {
502  return _reference;
503  }
504  return false;
505  }
506 
507 #define GET_OBJECT_FUNC(objectName) \
508  objectName *get ## objectName() { \
509  if (_xobject) \
510  return static_cast<objectName *>(_xobject->_object); \
511  else \
512  return nullptr; \
513  }
514 
515  GET_OBJECT_FUNC(XAnimTicksPerSecondObject)
516  GET_OBJECT_FUNC(XAnimationKeyObject)
517  GET_OBJECT_FUNC(XAnimationObject)
518  GET_OBJECT_FUNC(XAnimationOptionsObject)
519  GET_OBJECT_FUNC(XAnimationSetObject)
520  GET_OBJECT_FUNC(XDeclDataObject)
521  GET_OBJECT_FUNC(XFrameObject)
522  GET_OBJECT_FUNC(XFrameTransformMatrixObject)
523  GET_OBJECT_FUNC(XFVFDataObject)
524  GET_OBJECT_FUNC(XMaterialObject)
525  GET_OBJECT_FUNC(XMeshObject)
526  GET_OBJECT_FUNC(XMeshMaterialListObject)
527  GET_OBJECT_FUNC(XMeshNormalsObject)
528  GET_OBJECT_FUNC(XMeshVertexColorsObject)
529  GET_OBJECT_FUNC(XMeshTextureCoordsObject)
530  GET_OBJECT_FUNC(XSkinMeshHeaderObject)
531  GET_OBJECT_FUNC(XSkinWeightsObject)
532  GET_OBJECT_FUNC(XVertexDuplicationIndicesObject)
533  GET_OBJECT_FUNC(XTextureFilenameObject)
534 };
535 
537  friend class XFileLoader;
538 
539 private:
540 
541  XFileLoader *_file{};
542 
543 public:
544 
545  bool getChild(uint id, XFileData &child) {
546  if (_file) {
547  if (id < _file->_xobjects.size()) {
548  child._xobject = _file->_xobjects[id];
549  return true;
550  }
551  }
552  return false;
553  }
554 
555  bool getChildren(uint &num) {
556  if (_file) {
557  num = _file->_xobjects.size();
558  return true;
559  }
560  return false;
561  }
562 };
563 
564 } // namespace Wintermute
565 
566 #endif
Definition: xfile_loader.h:200
Definition: xfile_loader.h:128
Definition: xfile_loader.h:214
Definition: str.h:59
Definition: xfile_loader.h:170
Definition: xfile_loader.h:536
Definition: xfile_loader.h:228
Definition: xfile_loader.h:87
Definition: xfile_loader.h:262
bool isSpace(int c)
Definition: xfile_loader.h:92
Definition: xfile_loader.h:225
Definition: xfile_loader.h:103
Definition: xfile_loader.h:243
Definition: xfile_loader.h:377
Definition: xfile_loader.h:81
Definition: xfile_loader.h:97
Definition: xfile_loader.h:111
Definition: xfile_loader.h:158
Definition: xfile_loader.h:182
Definition: xfile_loader.h:191
Definition: xfile_loader.h:138
Definition: xfile_loader.h:118
Definition: xfile_loader.h:218
Definition: xfile_loader.h:144
Definition: xfile_loader.h:222
Definition: xfile_loader.h:246
Definition: xfile_loader.h:74
Definition: xfile_loader.h:297
Definition: xfile_loader.h:250
Definition: stack.h:102
Definition: xfile_loader.h:452
Definition: achievements_tables.h:27
Definition: xfile_loader.h:238