SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
Value.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <string>
7#include <string_view>
8#include <variant>
9#include <vector>
10
11#include <sourcepp/Math.h>
12
13namespace dmxpp {
14
15namespace Value {
16
17struct Invalid {};
18
19struct Element {
20 uint32_t index;
21 std::string stubGUID;
22};
23
24using ByteArray = std::vector<std::byte>;
25
26struct Time {
27 float seconds;
28};
29
30struct Color {
31 uint8_t r;
32 uint8_t g;
33 uint8_t b;
34 uint8_t a;
35};
36
37using Vector2 = sourcepp::math::Vec2f;
38
39using Vector3 = sourcepp::math::Vec3f;
40
41using Vector4 = sourcepp::math::Vec4f;
42
44
46
47using Matrix4x4 = sourcepp::math::Mat4x4f;
48
49using Generic = std::variant<
50 Invalid,
51 Element,
52 int32_t,
53 float,
54 bool,
55 std::string,
56 std::vector<std::byte>,
57 Time,
58 Color,
59 Vector2,
60 Vector3,
61 Vector4,
62 //EulerAngle, // Same as Vector3
63 //Quaternion, // Same as Vector4
65
66 std::vector<Element>,
67 std::vector<int32_t>,
68 std::vector<float>,
69 std::vector<bool>,
70 std::vector<std::string>,
71 std::vector<std::vector<std::byte>>,
72 std::vector<Time>,
73 std::vector<Color>,
74 std::vector<Vector2>,
75 std::vector<Vector3>,
76 std::vector<Vector4>,
77 //std::vector<EulerAngle>,
78 //std::vector<Quaternion>,
79 std::vector<Matrix4x4>
80>;
81
82enum class ID : uint8_t {
83 INVALID = 0,
84
85 VALUE_START = 1,
86 ELEMENT = 1,
87 INT = 2,
88 FLOAT = 3,
89 BOOL = 4,
90 STRING = 5,
91 BYTEARRAY = 6,
92 TIME = 7, // OBJECT_ID in v1 & v2
93 COLOR = 8,
94 VECTOR2 = 9,
95 VECTOR3 = 10,
96 VECTOR4 = 11,
97 EULER_ANGLE = 12,
98 QUATERNION = 13,
99 MATRIX_4X4 = 14,
100 VALUE_END = 14,
101
102 ARRAY_START = 15,
103 ARRAY_ELEMENT = 15,
104 ARRAY_INT = 16,
105 ARRAY_FLOAT = 17,
106 ARRAY_BOOL = 18,
107 ARRAY_STRING = 19,
108 ARRAY_BYTEARRAY = 20,
109 ARRAY_TIME = 21, // ARRAY_OBJECT_ID in v1 & v2
110 ARRAY_COLOR = 22,
111 ARRAY_VECTOR2 = 23,
112 ARRAY_VECTOR3 = 24,
113 ARRAY_VECTOR4 = 25,
115 ARRAY_QUATERNION = 27,
116 ARRAY_MATRIX_4X4 = 28,
117 ARRAY_END = 28,
118};
119
120constexpr std::byte IDToByte(ID id) {
121 return static_cast<std::byte>(id);
122}
123
124constexpr ID byteToID(std::byte id) {
125 return static_cast<ID>(id);
126}
127
128constexpr ID arrayIDToInnerID(ID id) {
129 if (id >= ID::ARRAY_START) {
130 return static_cast<ID>(static_cast<uint8_t>(id) - static_cast<uint8_t>(ID::VALUE_END));
131 }
132 return id;
133}
134
135constexpr ID innerIDToArrayID(ID id) {
136 if (id <= ID::VALUE_END) {
137 return static_cast<ID>(static_cast<uint8_t>(id) + static_cast<uint8_t>(ID::VALUE_END));
138 }
139 return id;
140}
141
142std::string IDToString(ID id);
143
144// NOLINTNEXTLINE(*-no-recursion)
145constexpr ID stringToID(std::string_view id) {
146 if (id == "element") return ID::ELEMENT;
147 if (id == "int") return ID::INT;
148 if (id == "float") return ID::FLOAT;
149 if (id == "bool") return ID::BOOL;
150 if (id == "string") return ID::STRING;
151 if (id == "binary") return ID::BYTEARRAY;
152 if (id == "time") return ID::TIME;
153 if (id == "color") return ID::COLOR;
154 if (id == "vector2") return ID::VECTOR2;
155 if (id == "vector3") return ID::VECTOR3;
156 if (id == "vector4") return ID::VECTOR4;
157 if (id == "quaternion") return ID::QUATERNION;
158 if (id == "matrix") return ID::MATRIX_4X4;
159 if (id.ends_with("_array")) {
160 return innerIDToArrayID(stringToID(id.substr(0, id.length() - 6)));
161 }
162 return ID::INVALID;
163}
164
165} // namespace Value
166
168 std::string name;
171
172 [[nodiscard]] bool isArray() const;
173
174 [[nodiscard]] std::string getValue() const;
175
176 template<typename T>
177 [[nodiscard]] T getValueAs() const {
178 return std::get<T>(this->value);
179 }
180};
181
183 std::string type;
184 std::string name;
185 std::array<std::byte, 16> guid;
186 std::vector<DMXAttribute> attributes;
187};
188
189} // namespace dmxpp
std::string IDToString(ID id)
Definition: Value.cpp:8
constexpr ID stringToID(std::string_view id)
Definition: Value.h:145
constexpr ID arrayIDToInnerID(ID id)
Definition: Value.h:128
sourcepp::math::Mat4x4f Matrix4x4
Definition: Value.h:47
sourcepp::math::Quat Quaternion
Definition: Value.h:45
std::vector< std::byte > ByteArray
Definition: Value.h:24
std::variant< Invalid, Element, int32_t, float, bool, std::string, std::vector< std::byte >, Time, Color, Vector2, Vector3, Vector4, Matrix4x4, std::vector< Element >, std::vector< int32_t >, std::vector< float >, std::vector< bool >, std::vector< std::string >, std::vector< std::vector< std::byte > >, std::vector< Time >, std::vector< Color >, std::vector< Vector2 >, std::vector< Vector3 >, std::vector< Vector4 >, std::vector< Matrix4x4 > > Generic
Definition: Value.h:80
sourcepp::math::Vec4f Vector4
Definition: Value.h:41
sourcepp::math::Vec2f Vector2
Definition: Value.h:37
sourcepp::math::EulerAngles EulerAngles
Definition: Value.h:43
sourcepp::math::Vec3f Vector3
Definition: Value.h:39
constexpr ID innerIDToArrayID(ID id)
Definition: Value.h:135
constexpr std::byte IDToByte(ID id)
Definition: Value.h:120
constexpr ID byteToID(std::byte id)
Definition: Value.h:124
Definition: dmxpp.h:7
Vec4f Quat
Definition: Math.h:330
Vec3f EulerAngles
Definition: Math.h:328
T getValueAs() const
Definition: Value.h:177
Value::ID type
Definition: Value.h:169
std::string getValue() const
Definition: Value.cpp:62
std::string name
Definition: Value.h:168
bool isArray() const
Definition: Value.cpp:58
Value::Generic value
Definition: Value.h:170
std::array< std::byte, 16 > guid
Definition: Value.h:185
std::vector< DMXAttribute > attributes
Definition: Value.h:186
std::string name
Definition: Value.h:184
std::string type
Definition: Value.h:183
std::string stubGUID
Definition: Value.h:21
uint32_t index
Definition: Value.h:20
float seconds
Definition: Value.h:27