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