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_IDESERIALIZER
19 : #define INCLUDED_IDESERIALIZER
20 :
21 : #include "maths/Fixed.h"
22 : #include "ps/Errors.h"
23 :
24 : #include <cstddef>
25 : #include <cstdint>
26 : #include <iosfwd>
27 : #include <js/TypeDecls.h>
28 : #include <string>
29 :
30 : ERROR_GROUP(Deserialize);
31 : ERROR_TYPE(Deserialize, OutOfBounds);
32 : ERROR_TYPE(Deserialize, InvalidCharInString);
33 : ERROR_TYPE(Deserialize, ReadFailed);
34 : ERROR_TYPE(Deserialize, ScriptError);
35 :
36 : /**
37 : * Deserialization interface; see \ref serialization "serialization overview".
38 : */
39 96 : class IDeserializer
40 : {
41 : public:
42 : virtual ~IDeserializer();
43 :
44 : virtual void NumberU8(const char* name, uint8_t& out, uint8_t lower, uint8_t upper);
45 : virtual void NumberI8(const char* name, int8_t& out, int8_t lower, int8_t upper);
46 : virtual void NumberU16(const char* name, uint16_t& out, uint16_t lower, uint16_t upper);
47 : virtual void NumberI16(const char* name, int16_t& out, int16_t lower, int16_t upper);
48 : virtual void NumberU32(const char* name, uint32_t& out, uint32_t lower, uint32_t upper);
49 : virtual void NumberI32(const char* name, int32_t& out, int32_t lower, int32_t upper);
50 : virtual void NumberU8_Unbounded(const char* name, uint8_t& out);
51 : virtual void NumberI8_Unbounded(const char* name, int8_t& out);
52 : virtual void NumberU16_Unbounded(const char* name, uint16_t& out);
53 : virtual void NumberI16_Unbounded(const char* name, int16_t& out);
54 : virtual void NumberU32_Unbounded(const char* name, uint32_t& out);
55 : virtual void NumberI32_Unbounded(const char* name, int32_t& out);
56 : virtual void NumberFloat_Unbounded(const char* name, float& out);
57 : virtual void NumberDouble_Unbounded(const char* name, double& out);
58 : virtual void NumberFixed_Unbounded(const char* name, fixed& out);
59 : virtual void Bool(const char* name, bool& out);
60 : virtual void StringASCII(const char* name, std::string& out, uint32_t minlength, uint32_t maxlength);
61 : virtual void String(const char* name, std::wstring& out, uint32_t minlength, uint32_t maxlength);
62 :
63 : /// Deserialize a JS::Value, replacing 'out'
64 : virtual void ScriptVal(const char* name, JS::MutableHandleValue out) = 0;
65 :
66 : /**
67 : * Deserialize an object and assign its properties to objVal
68 : * (Essentially equivalent to Object.assign(objVal, serialized))
69 : */
70 : virtual void ScriptObjectAssign(const char* name, JS::HandleValue objVal) = 0;
71 :
72 : /// Deserialize a JSString
73 : virtual void ScriptString(const char* name, JS::MutableHandleString out) = 0;
74 :
75 : virtual void RawBytes(const char* name, std::uint8_t* data, size_t len);
76 :
77 : // Features for simulation-state serialisation:
78 : virtual int GetVersion() const;
79 :
80 : /**
81 : * Returns a stream which can be used to deserialize data directly.
82 : * (This is particularly useful for chaining multiple deserializers
83 : * together.)
84 : */
85 : virtual std::istream& GetStream() = 0;
86 :
87 : /**
88 : * Throws an exception if the stream definitely cannot provide the required
89 : * number of bytes.
90 : * (It might be conservative and *not* throw an exception in some cases where
91 : * the stream actually can't provide the required bytes.)
92 : * (This should be used when allocating memory based on data in the
93 : * stream, e.g. reading strings, to avoid dangerously large allocations
94 : * when the data is invalid.)
95 : */
96 : virtual void RequireBytesInStream(size_t numBytes) = 0;
97 :
98 : protected:
99 : virtual void Get(const char* name, std::uint8_t* data, size_t len) = 0;
100 : };
101 :
102 : #endif // INCLUDED_IDESERIALIZER
103 :
|