Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef INCLUDED_BINARYSERIALIZER
19 : #define INCLUDED_BINARYSERIALIZER
20 :
21 : #include "ISerializer.h"
22 :
23 : #include "lib/byte_order.h"
24 : #include "lib/code_annotation.h"
25 : #include "lib/debug.h"
26 : #include "maths/Fixed.h"
27 : #include "simulation2/system/Component.h"
28 :
29 : #include <cstddef>
30 : #include <cstdint>
31 : #include <js/AllocPolicy.h>
32 : #include <js/GCHashTable.h>
33 : #include <js/Id.h>
34 : #include <js/RootingAPI.h>
35 : #include <js/TypeDecls.h>
36 : #include <memory>
37 : #include <ostream>
38 : #include <string>
39 :
40 : class JSTracer;
41 : namespace Script { class Interface; }
42 : namespace Script { class Request; }
43 :
44 : /**
45 : * Wrapper for redirecting ostream writes to CBinarySerializer's impl
46 : */
47 : template<typename T>
48 0 : class CSerializerStreamBuf : public std::streambuf
49 : {
50 : NONCOPYABLE(CSerializerStreamBuf);
51 : T& m_SerializerImpl;
52 : char m_Buffer[2048];
53 : public:
54 156 : CSerializerStreamBuf(T& impl) :
55 312 : m_SerializerImpl(impl)
56 : {
57 156 : setp(m_Buffer, m_Buffer + ARRAY_SIZE(m_Buffer) - 1);
58 : }
59 :
60 : protected:
61 : // Override overflow and sync, because older versions of libc++ streams
62 : // write strings as individual characters, then xsputn is never called
63 0 : int overflow(int ch)
64 : {
65 0 : if (ch == traits_type::eof())
66 : return traits_type::not_eof(ch);
67 :
68 0 : ENSURE(pptr() <= epptr());
69 0 : *pptr() = ch;
70 0 : pbump(1);
71 0 : sync();
72 0 : return ch;
73 : }
74 :
75 10 : int sync()
76 : {
77 10 : std::ptrdiff_t n = pptr() - pbase();
78 10 : if (n != 0)
79 : {
80 0 : pbump(-n);
81 0 : m_SerializerImpl.Put("stream", reinterpret_cast<const std::uint8_t*>(pbase()), n);
82 : }
83 10 : return 0;
84 : }
85 :
86 3 : std::streamsize xsputn(const char* s, std::streamsize n)
87 : {
88 3 : m_SerializerImpl.Put("stream", reinterpret_cast<const std::uint8_t*>(s), n);
89 3 : return n;
90 : }
91 : };
92 :
93 : /**
94 : * PutScriptVal implementation details.
95 : * (Split out from the main class because it's too big to be inlined.)
96 : */
97 : class CBinarySerializerScriptImpl
98 : {
99 : public:
100 : CBinarySerializerScriptImpl(const Script::Interface& scriptInterface, ISerializer& serializer);
101 : ~CBinarySerializerScriptImpl();
102 :
103 : void PutScriptVal(JS::HandleValue val);
104 : private:
105 : void ScriptString(const Script::Request& rq, const char* name, JS::HandleString string);
106 : void HandleScriptVal(const Script::Request& rq, JS::HandleValue val);
107 : static void Trace(JSTracer* trc, void* data);
108 :
109 : const Script::Interface& m_ScriptInterface;
110 : ISerializer& m_Serializer;
111 :
112 : using ObjectTagMap = JS::GCHashMap<JS::Heap<JSObject*>, std::uint32_t,
113 : js::StableCellHasher<JSObject*>, js::SystemAllocPolicy>;
114 : ObjectTagMap m_ScriptBackrefTags;
115 : std::uint32_t m_ScriptBackrefsNext;
116 : std::uint32_t GetScriptBackrefTag(const Script::Request& rq, JS::HandleObject obj);
117 :
118 : JS::PropertyKey m_SerializePropId;
119 : JS::PropertyKey m_DeserializePropId;
120 :
121 : };
122 :
123 : /**
124 : * Serialize to a binary stream. T must just implement the Put() method.
125 : * (We use this templated approach to allow compiler inlining.)
126 : */
127 : template <typename T>
128 : class CBinarySerializer : public ISerializer
129 : {
130 : NONCOPYABLE(CBinarySerializer);
131 : public:
132 24 : CBinarySerializer(const Script::Interface& scriptInterface) :
133 24 : m_ScriptImpl(new CBinarySerializerScriptImpl(scriptInterface, *this)),
134 24 : m_RawStreamBuf(m_Impl),
135 48 : m_RawStream(&m_RawStreamBuf)
136 : {
137 24 : }
138 :
139 : template <typename A>
140 132 : CBinarySerializer(const Script::Interface& scriptInterface, A& a) :
141 132 : m_ScriptImpl(new CBinarySerializerScriptImpl(scriptInterface, *this)),
142 132 : m_Impl(a),
143 132 : m_RawStreamBuf(m_Impl),
144 264 : m_RawStream(&m_RawStreamBuf)
145 : {
146 132 : }
147 :
148 : protected:
149 : /*
150 : The Put* implementations here are designed for subclasses
151 : that want an efficient, portable, deserializable representation.
152 : (Subclasses with different requirements should override these methods.)
153 :
154 : Numbers are converted to little-endian byte strings, for portability
155 : and efficiency.
156 :
157 : Data is not aligned, for storage efficiency.
158 : */
159 :
160 295763 : virtual void PutNumber(const char* name, uint8_t value)
161 : {
162 295763 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&value), sizeof(uint8_t));
163 295763 : }
164 :
165 1033 : virtual void PutNumber(const char* name, int8_t value)
166 : {
167 1033 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&value), sizeof(int8_t));
168 1033 : }
169 :
170 2659 : virtual void PutNumber(const char* name, uint16_t value)
171 : {
172 2659 : uint16_t v = to_le16(value);
173 2659 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&v), sizeof(uint16_t));
174 2659 : }
175 :
176 1 : virtual void PutNumber(const char* name, int16_t value)
177 : {
178 1 : int16_t v = static_cast<std::int16_t>(to_le16(static_cast<std::uint16_t>(value)));
179 1 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&v), sizeof(int16_t));
180 1 : }
181 :
182 320531 : virtual void PutNumber(const char* name, uint32_t value)
183 : {
184 320531 : uint32_t v = to_le32(value);
185 320531 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&v), sizeof(uint32_t));
186 320531 : }
187 :
188 41407 : virtual void PutNumber(const char* name, int32_t value)
189 : {
190 41407 : int32_t v = static_cast<std::int32_t>(to_le32(static_cast<std::uint32_t>(value)));
191 41407 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&v), sizeof(int32_t));
192 41407 : }
193 :
194 1 : virtual void PutNumber(const char* name, float value)
195 : {
196 1 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&value), sizeof(float));
197 1 : }
198 :
199 3937 : virtual void PutNumber(const char* name, double value)
200 : {
201 3937 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&value), sizeof(double));
202 3937 : }
203 :
204 45493 : virtual void PutNumber(const char* name, fixed value)
205 : {
206 45493 : int32_t v = static_cast<std::int32_t>(to_le32(static_cast<std::uint32_t>(
207 : value.GetInternalValue())));
208 45493 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(&v), sizeof(int32_t));
209 45493 : }
210 :
211 13746 : virtual void PutBool(const char* name, bool value)
212 : {
213 18729 : NumberU8(name, value ? 1 : 0, 0, 1);
214 13746 : }
215 :
216 30735 : virtual void PutString(const char* name, const std::string& value)
217 : {
218 : // TODO: maybe should intern strings, particularly to save space with script property names
219 30735 : PutNumber("string length", (uint32_t)value.length());
220 30735 : m_Impl.Put(name, reinterpret_cast<const std::uint8_t*>(value.data()), value.length());
221 30735 : }
222 :
223 25201 : virtual void PutScriptVal(const char* /*name*/, JS::MutableHandleValue value)
224 : {
225 25201 : m_ScriptImpl->PutScriptVal(value);
226 25198 : }
227 :
228 122736 : virtual void PutRaw(const char* name, const std::uint8_t* data, size_t len)
229 : {
230 122736 : m_Impl.Put(name, data, len);
231 122736 : }
232 :
233 10 : virtual std::ostream& GetStream()
234 : {
235 10 : return m_RawStream;
236 : }
237 :
238 : protected:
239 : T m_Impl;
240 :
241 : private:
242 : std::unique_ptr<CBinarySerializerScriptImpl> m_ScriptImpl;
243 :
244 : CSerializerStreamBuf<T> m_RawStreamBuf;
245 : std::ostream m_RawStream;
246 : };
247 :
248 : #endif // INCLUDED_BINARYSERIALIZER
|