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