SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataStorage.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt for details)
6  */
7 
8 #pragma once
9 
10 #include <glm/vec3.hpp>
11 #include <glm/vec2.hpp>
12 #include <string>
13 #include <vector>
14 #include <QtCore/QString>
15 #include <QtCore/QFile>
16 #include <fstream>
17 #include <map>
18 #include <type_traits>
19 struct RGBA;
20 
21 typedef glm::vec3 Vec3;
22 typedef glm::vec2 Vec2;
23 class BinStore // binary storage
24 {
25  struct FileEntry {
26  QString name;
27  uint32_t date=0;
28  };
29  QFile m_str;
30  size_t bytes_read=0;
31  uint32_t bytes_to_read=0;
32  std::vector<uint32_t> m_file_sizes; // implicit stack
33  std::vector<FileEntry> m_entries;
34 
35  template<class V>
36  size_t read_internal(V &res)
37  {
38  if(m_file_sizes.size()>0 && current_fsize()<sizeof(V))
39  return 0;
40  m_str.read((char *)&res,sizeof(V));
41  if(m_file_sizes.size()>0)
42  {
43  bytes_read+=sizeof(V);
44  (*m_file_sizes.rbegin())-=sizeof(V);
45  }
46  return sizeof(V);
47  }
48  QString read_pstr(size_t maxlen);
49  void skip_pstr();
50  bool read_data_blocks(bool file_data_blocks);
51  bool check_bin_version_and_crc(uint32_t req_crc);
52  uint32_t current_fsize() {return *m_file_sizes.rbegin();}
53  uint32_t read_header(QString & name, size_t maxlen);
54  void fixup();
55 public:
56 
57  QString source_name() {
58  return read_str(12000);
59  }
60  bool read_bytes(char *tgt,size_t sz);
61  QString read_str(size_t maxlen);
62  bool read(uint32_t &v);
63  bool read(int32_t &v);
64  bool read(float &v);
65  bool read(uint16_t &v);
66  bool read(uint8_t &v);
67  bool read(Vec2 &v);
68  bool read(Vec3 &v);
69  bool read(RGBA &v);
70  bool read(std::vector<uint32_t> &v);
71  bool read(std::vector<int32_t> &res);
72  bool read(std::vector<float> &res);
73  bool read(std::vector<QString> &res);
74  bool read(std::vector<std::vector<QString>> &res);
75  bool read(uint8_t *&val, uint32_t length);
76  bool read(QString &val);
77  template<class Enum>
78  bool readEnum(Enum &val)
79  {
80  typename std::underlying_type<Enum>::type true_val;
81  if(!read(true_val))
82  return false;
83  val = Enum(true_val);
84  return true;
85  }
86  template<class Enum>
87  bool readEnum(std::vector<Enum> &val)
88  {
89  std::vector<typename std::underlying_type<Enum>::type> true_val;
90  if(!read(true_val))
91  return false;
92  // this is bad, and I feel bad for writing it :/
93  val = std::move(*(std::vector<Enum> *)(&true_val));
94  return true;
95  }
96  void prepare();
97  bool prepare_nested();
98  bool nesting_name(QString & name);
99  void nest_in() { }
100  void nest_out() { m_file_sizes.pop_back(); }
101  bool end_encountered() const;
102  bool open(const QString & name, uint32_t required_crc);
103  bool findAndOpen(const QString & name, uint32_t reqcrc=0);
104 };
Definition: Colors.h:62
bool readEnum(Enum &val)
Definition: DataStorage.h:78
bool prepare_nested()
Definition: DataStorage.cpp:290
bool open(const QString &name, uint32_t required_crc)
Definition: DataStorage.cpp:97
glm::vec2 Vec2
Definition: DataStorage.h:22
QString source_name()
Definition: DataStorage.h:57
QString read_str(size_t maxlen)
Definition: DataStorage.cpp:268
Definition: DataStorage.h:23
bool readEnum(std::vector< Enum > &val)
Definition: DataStorage.h:87
void nest_in()
Definition: DataStorage.h:99
glm::vec3 Vec3
Definition: DataStorage.h:19
void nest_out()
Definition: DataStorage.h:100
bool findAndOpen(const QString &name, uint32_t reqcrc=0)
Definition: DataStorage.cpp:109
void prepare()
Definition: DataStorage.cpp:275
bool read(uint32_t &v)
Definition: DataStorage.cpp:123
bool end_encountered() const
Definition: DataStorage.cpp:323
bool read_bytes(char *tgt, size_t sz)
Definition: DataStorage.cpp:261
bool nesting_name(QString &name)
Definition: DataStorage.cpp:298