Line data Source code
1 : /* Copyright (C) 2025 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 : #include "precompiled.h"
19 :
20 : #include "ISerializer.h"
21 :
22 : #include "lib/status.h"
23 : #include "lib/utf8.h"
24 : #include "simulation2/system/Component.h"
25 :
26 : #include <js/RootingAPI.h>
27 :
28 177 : ISerializer::~ISerializer()
29 : {
30 177 : }
31 :
32 15870 : void ISerializer::NumberU8(const char* name, uint8_t value, uint8_t lower, uint8_t upper)
33 : {
34 15870 : if (!(lower <= value && value <= upper))
35 0 : throw PSERROR_Serialize_OutOfBounds();
36 15870 : PutNumber(name, value);
37 15870 : }
38 :
39 0 : void ISerializer::NumberI8(const char* name, int8_t value, int8_t lower, int8_t upper)
40 : {
41 0 : if (!(lower <= value && value <= upper))
42 0 : throw PSERROR_Serialize_OutOfBounds();
43 0 : PutNumber(name, value);
44 0 : }
45 :
46 0 : void ISerializer::NumberU16(const char* name, uint16_t value, uint16_t lower, uint16_t upper)
47 : {
48 0 : if (!(lower <= value && value <= upper))
49 0 : throw PSERROR_Serialize_OutOfBounds();
50 0 : PutNumber(name, value);
51 0 : }
52 :
53 0 : void ISerializer::NumberI16(const char* name, int16_t value, int16_t lower, int16_t upper)
54 : {
55 0 : if (!(lower <= value && value <= upper))
56 0 : throw PSERROR_Serialize_OutOfBounds();
57 0 : PutNumber(name, value);
58 0 : }
59 :
60 2057 : void ISerializer::NumberU32(const char* name, uint32_t value, uint32_t lower, uint32_t upper)
61 : {
62 2057 : if (!(lower <= value && value <= upper))
63 0 : throw PSERROR_Serialize_OutOfBounds();
64 2057 : PutNumber(name, value);
65 2057 : }
66 :
67 9 : void ISerializer::NumberI32(const char* name, int32_t value, int32_t lower, int32_t upper)
68 : {
69 9 : if (!(lower <= value && value <= upper))
70 2 : throw PSERROR_Serialize_OutOfBounds();
71 7 : PutNumber(name, value);
72 7 : }
73 :
74 6008 : void ISerializer::StringASCII(const char* name, const std::string& value, uint32_t minlength, uint32_t maxlength)
75 : {
76 6008 : if (!(minlength <= value.length() && value.length() <= maxlength))
77 0 : throw PSERROR_Serialize_OutOfBounds();
78 :
79 56195 : for (size_t i = 0; i < value.length(); ++i)
80 50187 : if (value[i] == 0 || (unsigned char)value[i] >= 128)
81 0 : throw PSERROR_Serialize_InvalidCharInString();
82 :
83 6008 : PutString(name, value);
84 6008 : }
85 :
86 24739 : void ISerializer::String(const char* name, const std::wstring& value, uint32_t minlength, uint32_t maxlength)
87 : {
88 24739 : if (!(minlength <= value.length() && value.length() <= maxlength))
89 0 : throw PSERROR_Serialize_OutOfBounds();
90 :
91 24739 : Status err;
92 24739 : std::string str = utf8_from_wstring(value, &err);
93 24739 : if (err)
94 0 : throw PSERROR_Serialize_InvalidCharInString();
95 :
96 24739 : PutString(name, str);
97 24739 : }
98 :
99 25210 : void ISerializer::ScriptVal(const char* name, JS::MutableHandleValue value)
100 : {
101 25210 : PutScriptVal(name, value);
102 25207 : }
103 :
104 122737 : void ISerializer::RawBytes(const char* name, const std::uint8_t* data, size_t len)
105 : {
106 122737 : PutRaw(name, data, len);
107 122737 : }
108 :
109 2094 : bool ISerializer::IsDebug() const
110 : {
111 2094 : return false;
112 : }
|