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 : #include "precompiled.h"
19 :
20 : #include "DebugSerializer.h"
21 :
22 : #include "lib/debug.h"
23 : #include "lib/secure_crt.h"
24 : #include "ps/CStr.h"
25 : #include "scriptinterface/FunctionWrapper.h"
26 : #include "scriptinterface/JSON.h"
27 : #include "scriptinterface/Object.h"
28 : #include "scriptinterface/Request.h"
29 :
30 : #include <iomanip>
31 : #include <js/RootingAPI.h>
32 : #include <js/Value.h>
33 : #include <sstream>
34 :
35 : /*
36 : * The output format here is intended to be compatible with YAML,
37 : * so it is human readable and usable in diff and can also be parsed with
38 : * external tools.
39 : */
40 :
41 : // MSVC and GCC give slightly different serializations of floats
42 : // (e.g. "1e+010" vs "1e+10"). To make the debug serialization easily comparable
43 : // across platforms, we want to convert to a canonical form.
44 : // TODO: we just do e+0xx now; ought to handle varying precisions and inf and nan etc too
45 : template<typename T>
46 20 : std::string canonfloat(T value, int prec)
47 : {
48 20 : std::stringstream str;
49 20 : str << std::setprecision(prec) << value;
50 20 : std::string r = str.str();
51 20 : size_t e = r.find('e');
52 20 : if (e == r.npos) // no "e"
53 : return r;
54 12 : if (e == r.length() - 5 && r[e + 2] == '0') // e.g. "1e+010"
55 0 : r.erase(e + 2, 1);
56 : return r;
57 20 : }
58 :
59 21 : CDebugSerializer::CDebugSerializer(const Script::Interface& scriptInterface, std::ostream& stream, bool includeDebugInfo) :
60 21 : m_ScriptInterface(scriptInterface), m_Stream(stream), m_IsDebug(includeDebugInfo), m_Indent(0)
61 : {
62 21 : }
63 :
64 8 : void CDebugSerializer::Indent(int spaces)
65 : {
66 8 : m_Indent += spaces;
67 8 : }
68 :
69 8 : void CDebugSerializer::Dedent(int spaces)
70 : {
71 8 : ENSURE(spaces <= m_Indent);
72 8 : m_Indent -= spaces;
73 8 : }
74 :
75 : #define INDENT std::string(m_Indent, ' ')
76 :
77 1 : void CDebugSerializer::Comment(const std::string& comment)
78 : {
79 1 : m_Stream << INDENT << "# " << comment << "\n";
80 1 : }
81 :
82 25 : void CDebugSerializer::TextLine(const std::string& text)
83 : {
84 25 : m_Stream << INDENT << text << "\n";
85 25 : }
86 :
87 1 : void CDebugSerializer::PutNumber(const char* name, uint8_t value)
88 : {
89 1 : m_Stream << INDENT << name << ": " << (int)value << "\n";
90 1 : }
91 :
92 1 : void CDebugSerializer::PutNumber(const char* name, int8_t value)
93 : {
94 1 : m_Stream << INDENT << name << ": " << (int)value << "\n";
95 1 : }
96 :
97 9 : void CDebugSerializer::PutNumber(const char* name, uint16_t value)
98 : {
99 9 : m_Stream << INDENT << name << ": " << value << "\n";
100 9 : }
101 :
102 1 : void CDebugSerializer::PutNumber(const char* name, int16_t value)
103 : {
104 1 : m_Stream << INDENT << name << ": " << value << "\n";
105 1 : }
106 :
107 43 : void CDebugSerializer::PutNumber(const char* name, uint32_t value)
108 : {
109 43 : m_Stream << INDENT << name << ": " << value << "\n";
110 43 : }
111 :
112 13 : void CDebugSerializer::PutNumber(const char* name, int32_t value)
113 : {
114 13 : m_Stream << INDENT << name << ": " << value << "\n";
115 13 : }
116 :
117 9 : void CDebugSerializer::PutNumber(const char* name, float value)
118 : {
119 27 : m_Stream << INDENT << name << ": " << canonfloat(value, 8) << "\n";
120 9 : }
121 :
122 11 : void CDebugSerializer::PutNumber(const char* name, double value)
123 : {
124 33 : m_Stream << INDENT << name << ": " << canonfloat(value, 17) << "\n";
125 11 : }
126 :
127 56 : void CDebugSerializer::PutNumber(const char* name, fixed value)
128 : {
129 168 : m_Stream << INDENT << name << ": " << value.ToString() << "\n";
130 56 : }
131 :
132 20 : void CDebugSerializer::PutBool(const char* name, bool value)
133 : {
134 31 : m_Stream << INDENT << name << ": " << (value ? "true" : "false") << "\n";
135 20 : }
136 :
137 12 : void CDebugSerializer::PutString(const char* name, const std::string& value)
138 : {
139 12 : std::string escaped;
140 12 : escaped.reserve(value.size());
141 99 : for (size_t i = 0; i < value.size(); ++i)
142 87 : if (value[i] == '"')
143 2 : escaped += "\\\"";
144 85 : else if (value[i] == '\\')
145 1 : escaped += "\\\\";
146 84 : else if (value[i] == '\n')
147 2 : escaped += "\\n";
148 : else
149 169 : escaped += value[i];
150 :
151 24 : m_Stream << INDENT << name << ": " << "\"" << escaped << "\"\n";
152 12 : }
153 :
154 9 : void CDebugSerializer::PutScriptVal(const char* name, JS::MutableHandleValue value)
155 : {
156 9 : Script::Request rq(m_ScriptInterface);
157 :
158 9 : JS::RootedValue serialize(rq.cx);
159 9 : if (Script::GetProperty(rq, value, "Serialize", &serialize) && !serialize.isNullOrUndefined())
160 : {
161 : // If the value has a Serialize property, pretty-parse that instead.
162 : // (this gives more accurate OOS reports).
163 1 : Script::Function::Call(rq, value, "Serialize", &serialize);
164 1 : std::string serialized_source = Script::ToString(rq, &serialize, true);
165 2 : m_Stream << INDENT << name << ": " << serialized_source << "\n";
166 1 : }
167 : // null is used explicitly as "nothing to serialize" so skip it even in debug
168 8 : else if (serialize.isUndefined())
169 : {
170 8 : std::string source = Script::ToString(rq, value, true);
171 16 : m_Stream << INDENT << name << ": " << source << "\n";
172 8 : }
173 9 : }
174 :
175 1 : void CDebugSerializer::PutRaw(const char* name, const std::uint8_t* data, size_t len)
176 : {
177 1 : m_Stream << INDENT << name << ": (" << len << " bytes)";
178 :
179 1 : char buf[4];
180 7 : for (size_t i = 0; i < len; ++i)
181 : {
182 6 : sprintf_s(buf, ARRAY_SIZE(buf), " %02x", (unsigned int)data[i]);
183 6 : m_Stream << buf;
184 : }
185 :
186 1 : m_Stream << "\n";
187 1 : }
188 :
189 6 : bool CDebugSerializer::IsDebug() const
190 : {
191 6 : return m_IsDebug;
192 : }
193 :
194 0 : std::ostream& CDebugSerializer::GetStream()
195 : {
196 0 : return m_Stream;
197 : }
|