GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
BinarySerializer.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 
7 #pragma once
8 
9 #include <cstdint>
10 #include <cstring>
11 #include <vector>
12 
13 #include "GDCore/Serialization/SerializerElement.h"
14 
15 namespace gd {
16 
23 class GD_CORE_API BinarySerializer {
24  public:
31  static void SerializeToBinaryBuffer(const SerializerElement& element,
32  std::vector<uint8_t>& outBuffer);
33 
42  static bool DeserializeFromBinaryBuffer(const uint8_t* buffer,
43  size_t bufferSize,
44  SerializerElement& outElement);
45 
49 
54  static uintptr_t CreateBinarySnapshot(const SerializerElement& element);
55 
60  static size_t GetLastBinarySnapshotSize();
61 
65  static void FreeBinarySnapshot(uintptr_t bufferPtr);
66 
71  static SerializerElement* DeserializeBinarySnapshot(uintptr_t bufferPtr,
72  size_t size);
74 
75  enum class NodeType : uint8_t {
76  Element = 0x01,
77  ValueUndefined = 0x02,
78  ValueBool = 0x03,
79  ValueInt = 0x04,
80  ValueDouble = 0x05,
81  ValueString = 0x06
82  };
83 
84  private:
85  // Internal serialization
86  static void SerializeElement(const SerializerElement& element,
87  std::vector<uint8_t>& buffer);
88  static void SerializeValue(const SerializerValue& value,
89  std::vector<uint8_t>& buffer);
90  static void SerializeString(const gd::String& str,
91  std::vector<uint8_t>& buffer);
92 
93  // Internal deserialization
94  static bool DeserializeElement(const uint8_t*& ptr,
95  const uint8_t* end,
96  SerializerElement& element);
97  static bool DeserializeValue(const uint8_t*& ptr,
98  const uint8_t* end,
99  SerializerValue& value,
100  NodeType valueType);
101  static bool DeserializeString(const uint8_t*& ptr,
102  const uint8_t* end,
103  gd::String& str);
104 
105  // Helper to write primitive types
106  template <typename T>
107  static void Write(std::vector<uint8_t>& buffer, const T& value) {
108  const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value);
109  buffer.insert(buffer.end(), bytes, bytes + sizeof(T));
110  }
111 
112  // Helper to read primitive types
113  template <typename T>
114  static bool Read(const uint8_t*& ptr, const uint8_t* end, T& value) {
115  if (ptr + sizeof(T) > end) return false;
116  std::memcpy(&value, ptr, sizeof(T));
117  ptr += sizeof(T);
118  return true;
119  }
120 
121  // Store the last binary size for retrieval
122  static size_t lastBinarySnapshotSize;
123 };
124 
125 } // namespace gd
Fast binary serialization/deserialization for SerializerElement trees.
Definition: BinarySerializer.h:23
A generic container that can represent a value ( containing a string, double, bool or int),...
Definition: SerializerElement.h:37
A value stored inside a gd::SerializerElement.
Definition: SerializerValue.h:20
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24