Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
cof.cpp
1 //
2 // Created by Lectem on 23/10/2016.
3 //
4 
5 #include "cof.h"
6 #include <FileStream.h>
7 #include <SystemUtils.h>
8 #include <fmt/format.h>
9 #include "utils.h"
10 
11 // TODO : Remove asserts and replace with proper error handling
12 
13 namespace WorldStone
14 {
15 
16 constexpr uint8_t COF::componentsNumber;
17 constexpr const char* COF::componentsNames[COF::componentsNumber];
18 
19 bool COF::read(const StreamPtr& streamPtr)
20 {
21  if (streamPtr && streamPtr->good()) {
22  static_assert(std::is_trivially_copyable<Header>(),
23  "COF::Header must be trivially copyable");
24  static_assert(sizeof(Header) == 4 + 6 * sizeof(uint32_t),
25  "COF::Header struct needs to be packed");
26  streamPtr->read(&header, sizeof(header));
27  if (streamPtr->fail() || header.version != 20) return false;
28 
29  static_assert(std::is_trivially_copyable<Layer>(), "COF::Layer must be trivially copyable");
30  static_assert(sizeof(Layer) == 9 * sizeof(uint8_t), "COF::Layer struct needs to be packed");
31  layers.resize(header.layers);
32  streamPtr->read(layers.data(), header.layers * sizeof(Layer));
33  for (const Layer& layer : layers)
34  {
35  WS_UNUSED(layer);
36  assert(layer.weaponClass[3] == '\0');
37  }
38 
39  keyframes.resize(header.frames);
40  streamPtr->read(keyframes.data(), header.frames);
41 
42  const size_t layersOrderSize = header.frames * header.directions * header.layers;
43  layersOrder.resize(layersOrderSize);
44  streamPtr->read(layersOrder.data(), layersOrderSize);
45 
46  return streamPtr->good();
47  }
48  return false;
49 }
50 
51 } // namespace WorldStone
uint8_t directions
Number of directions in the animation.
Definition: cof.h:31
uint8_t frames
Number of frames in the animation.
Definition: cof.h:30
uint8_t layers
Number of components layers.
Definition: cof.h:29
#define WS_UNUSED(x)
Mark a variable as unused to shut warnings when it is unused on purpose.
Definition: SystemUtils.h:21
uint8_t version
The version of the COF, always 20 in the game files.
Definition: cof.h:32
static constexpr uint8_t componentsNumber
Definition: cof.h:81